package cn.iocoder.yudao.module.product.controller.admin.resourceshare;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.product.controller.admin.resourceshare.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.resourceshare.ResourceShareDO;
import cn.iocoder.yudao.module.product.service.resourceshare.ResourceShareService;

@Tag(name = "管理后台 - 产品资源分享")
@RestController
@RequestMapping("/product/resource-share")
@Validated
public class ResourceShareController {

    @Resource
    private ResourceShareService resourceShareService;

    @PostMapping("/create")
    @Operation(summary = "创建产品资源分享")
    @PreAuthorize("@ss.hasPermission('product:resource-share:create')")
    public CommonResult<Long> createResourceShare(@Valid @RequestBody ResourceShareSaveReqVO createReqVO) {
        return success(resourceShareService.createResourceShare(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品资源分享")
    @PreAuthorize("@ss.hasPermission('product:resource-share:update')")
    public CommonResult<Boolean> updateResourceShare(@Valid @RequestBody ResourceShareSaveReqVO updateReqVO) {
        resourceShareService.updateResourceShare(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品资源分享")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:resource-share:delete')")
    public CommonResult<Boolean> deleteResourceShare(@RequestParam("id") Long id) {
        resourceShareService.deleteResourceShare(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品资源分享")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:resource-share:query')")
    public CommonResult<ResourceShareRespVO> getResourceShare(@RequestParam("id") Long id) {
        ResourceShareDO resourceShare = resourceShareService.getResourceShare(id);
        return success(BeanUtils.toBean(resourceShare, ResourceShareRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品资源分享分页")
    @PreAuthorize("@ss.hasPermission('product:resource-share:query')")
    public CommonResult<PageResult<ResourceShareRespVO>> getResourceSharePage(@Valid ResourceSharePageReqVO pageReqVO) {
        PageResult<ResourceShareDO> pageResult = resourceShareService.getResourceSharePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResourceShareRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品资源分享 Excel")
    @PreAuthorize("@ss.hasPermission('product:resource-share:export')")
    @OperateLog(type = EXPORT)
    public void exportResourceShareExcel(@Valid ResourceSharePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceShareDO> list = resourceShareService.getResourceSharePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品资源分享.xls", "数据", ResourceShareRespVO.class,
                        BeanUtils.toBean(list, ResourceShareRespVO.class));
    }

}